package com.hnu.model;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * A data access object (DAO) providing persistence and search support for
 * Bookmarkpreference entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.hnu.model.Bookmarkpreference
 * @author MyEclipse Persistence Tools
 */

public class BookmarkpreferenceDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory
			.getLogger(BookmarkpreferenceDAO.class);
	// property constants
	public static final String PREFERENCE = "preference";

	protected void initDao() {
		// do nothing
	}

	public List<Bookmark> getTopNBookmark(final int count, final long currentTime) {
		Object dataList = this.getHibernateTemplate().executeFind(
				new HibernateCallback() {

					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "SELECT bookmarkpreference.bookmark FROM Bookmarkpreference as bookmarkpreference GROUP BY bookmarkpreference.bookmark  ORDER BY (? - bookmarkpreference.bookmark.enterTime+2)/1000/3600/24/7/count(*)";
						Query q = session.createQuery(hql);
						q.setParameter(0,currentTime);
						q.setFirstResult(0);
						q.setMaxResults(count);
						return q.list();
					}
				});
		return (List<Bookmark>) dataList;
	}

	public List getBookmarkpreferenceByTwo(long userId, long bookmarkId) {
		String hql = "FROM Bookmarkpreference as bookmarkpreference WHERE bookmarkpreference.user.userId = ? AND bookmarkpreference.bookmark.bookmarkId = ?";
		return this.getHibernateTemplate().find(hql, userId, bookmarkId);
	}

	public void save(Bookmarkpreference transientInstance) {
		log.debug("saving Bookmarkpreference instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Bookmarkpreference persistentInstance) {
		log.debug("deleting Bookmarkpreference instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Bookmarkpreference findById(java.lang.Long id) {
		log.debug("getting Bookmarkpreference instance with id: " + id);
		try {
			Bookmarkpreference instance = (Bookmarkpreference) getHibernateTemplate()
					.get("com.hnu.model.Bookmarkpreference", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Bookmarkpreference instance) {
		log.debug("finding Bookmarkpreference instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Bookmarkpreference instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from Bookmarkpreference as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findAll() {
		log.debug("finding all Bookmarkpreference instances");
		try {
			String queryString = "from Bookmarkpreference";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Bookmarkpreference merge(Bookmarkpreference detachedInstance) {
		log.debug("merging Bookmarkpreference instance");
		try {
			Bookmarkpreference result = (Bookmarkpreference) getHibernateTemplate()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public static BookmarkpreferenceDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (BookmarkpreferenceDAO) ctx.getBean("BookmarkpreferenceDAO");
	}
}